package com.idrl.web.action;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.idrl.Const;
import com.idrl.dao.po.User;

public class LoginAction extends BaseAction {

	private static Logger log = Logger.getLogger(LoginAction.class);	
	
	private User user;
	
	private boolean cookieFlag = true;
	
	private static final int COOKIE_TIME = 60*60*24*365; 

	/** 登录成功后重定向地址 */
	private String redirectUrl;
	
	public String exit() {
		this.setUserToCookie(this.getUserFromSession(), 0);
		this.getRequest().getSession().invalidate();
		return "index";
	}
	
	public String loginByCookie()
	{
		user = this.getUserFromCookie();
		if ( user == null || StringUtils.isBlank(user.getEmail())
				|| StringUtils.isBlank(user.getPassword()))
		{
			return LOGIN;
		}
		return execute();
	}
	
	/** 登录 */
	public String execute() {				
		
		log.debug(user);		
		User vo = this.userService.findUniqueBy("email", user.getEmail());
		
		log.debug(vo);
		if (vo == null)
		{
			this.getRequest().setAttribute(Const.KEY_INFO_MSG,
					"此邮箱["+user.getEmail()+"]未注册！ <a href='"+ this.getUrlPath() + "/user/regAction!gotoReg.do?user.email=" + user.getEmail() + "'>现在去注册</a>");
			return LOGIN;
		}
//		else if (vo.getState() == User.INVALID)
//		{
//			this.getRequest().setAttribute(Const.KEY_INFO_MSG,
//					"此邮箱["+user.getEmail()+"]未激活！ <a href='"+ this.getUrlPath() +"/user/regAction!gotoActivate.do?user.semail=" + user.getEmail() + "'>发送激活邮件</a>");
//			return LOGIN;
//		}
		else if (vo.getState() == User.TEMP)
		{
			this.getRequest().setAttribute(Const.KEY_INFO_MSG,
					"临时用户["+user.getEmail()+"]不能登录！");
			return LOGIN;
		}
		
		if (vo.getPassword().equals(user.getPassword())) {
			
			
			this.setUserToSession(vo);
			redirectUrl = (String)this.getSession().get(Const.URL_INPUT);
			
			if( this.cookieFlag )
			{
				this.setUserToCookie(user, COOKIE_TIME);
			}
			else
			{
				this.setUserToCookie(user, 0); //删除Cookie
			}
			
			if(redirectUrl != null)
			{
				return "redirect";
			}
			return HOME;
		}
		else
		{
			this.getRequest().setAttribute(Const.KEY_INFO_MSG,
					"密码不正确！");
			return LOGIN;
		}
		
	}
	
	private void setUserToCookie(User user, int time)
	{
		Cookie nameCookie = new Cookie("ftname", user.getEmail().replace('@', '#')); 
		nameCookie.setMaxAge(time);
		Cookie pwdCookie = new Cookie("ftpwd", user.getPassword()); 
		pwdCookie.setMaxAge(time);
		
		this.getResponse().addCookie(nameCookie);
		this.getResponse().addCookie(pwdCookie);
	}
	
	private User getUserFromCookie()
	{
		Cookie[] cookies = this.getRequest().getCookies(); 
		User user = null;
		if (cookies != null) {
			user = new User();
			for (int i = 0; i < cookies.length; i++) {

				if (cookies[i].getName().equalsIgnoreCase("ftname")) {
					user.setEmail(cookies[i].getValue().replace('#', '@'));
				} else if (cookies[i].getName().equalsIgnoreCase("ftpwd")) {
					user.setPassword(cookies[i].getValue());
				}
			}
		}

		return user;
	}
	


	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public String getRedirectUrl() {
		return redirectUrl;
	}


	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}	

	
}
